//
//  LinkList.c
//  Learn_C_Cpp
//
//  Created by jinbang.li on 2023/6/12.
//

#include "LinkList.h"
#include <stdlib.h>
/**
 单链表的打印
 */
void SingleListPrint(SingleListNode *phead){
    SingleListNode* temp = phead;
    while (temp != NULL) {
        printf("data======%d\n",temp->data);
        temp = temp->next;
    }
}
/**
 尾插法
 */
void SingleListPushBack(SingleListNode **phead,ElementType x){
    SingleListNode *newNode = (SingleListNode*)malloc(sizeof(SingleListNode));
    if (newNode == NULL) {
        printf("无可用分配的内存空间\n");
    }
    newNode->data = x;
    newNode->next = NULL;
    SingleListNode *temp = *phead;
    while (temp != NULL) {
        temp = temp->next;
        if (temp->next == NULL) {
            temp->next = newNode;
            break;
        }
    }
}
void SingleListPushFront(SingleListNode **phead,ElementType x){
    SingleListNode *newNode = (SingleListNode*)malloc(sizeof(SingleListNode));
    if (newNode == NULL) {
        printf("无可用分配的内存空间\n");
    }
    newNode->data = x;
    newNode->next = *phead;
    *phead = newNode;//换头
}
void SingleListNodeInsert(SingleListNode **phead,size_t pos,ElementType x){
    
}

void SingleListPopBack(SingleListNode **phead){
    
}
/**
 删除头部
 */
void SingleListPopFront(SingleListNode **phead){
    SingleListNode *head = *phead;
    if (head == NULL) {
        printf("链表为空\n");
        return;
    }
    head = head->next;
    free(head);
}
void SingleListEraseAfter(SingleListNode **phead,size_t pos){
    
}

void SingleListUpdate(SingleListNode **phead,size_t pos,ElementType x){
    
}
/***/
SingleListNode* SLFind(SingleListNode *phead,ElementType x){
    SingleListNode* findNode = NULL;
    SingleListNode* temp = phead;
    while (temp != NULL) {
        if (temp->data == x) {
            findNode = phead;
            break;
        }
        temp = temp->next;
    }
    return temp;
}

void SListDestory(SingleListNode *phead){
    phead->next = NULL;
    free(phead);
}
